package com.example.badmintonWeb.controller;

import com.example.badmintonCommon.DTO.BookPlaceRequest;
import com.example.badmintonCommon.entity.Gym;
import com.example.badmintonCommon.entity.Order;
import com.example.badmintonCommon.entity.User;
import com.example.badmintonUser.Gym.GymServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.badmintonCommon.pojo.Result;
import com.example.badmintonUser.user.UserServer;

import java.util.List;

@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/user")
public class UserController {

    private  final UserServer userServer;
    private  final GymServer gymServer;
    @Autowired
    public UserController(UserServer userServer,GymServer gymServer) {
        this.userServer = userServer;
        this.gymServer=gymServer;
    }

    //用户通过手机号登录
    @PostMapping("/login")
    public Result<String> UserLogin(@RequestParam String tel,@RequestParam String psw){
        Result result=userServer.UserLogin(tel,psw);
        return result;
    }

    //用户通过手机号注册
    @PostMapping("/register")
    public Result<String> UserRegister(String tel,String psw, String name, String sex, String role,String islock){
        User user=new User(tel,psw,name,sex,role,islock);
        Result result=userServer.UserRegister(user);
        return result;
    }

    //用户获取全部场地信息
    @GetMapping("/FindAllGyms")
    public Result<List<Gym>> FindAllGyms(){
        Result result=gymServer.FindAllGyms();
        return result;
    }

    //用户预约球馆
    @PostMapping("/bookPlace")
    public Result<Gym> bookPlace(@RequestBody BookPlaceRequest bookPlaceRequest){
        Result result=gymServer.bookPlace(bookPlaceRequest);
        return result;
    }

    //用户取消预约球馆
    @PostMapping("/UnBookPlace")
    public Result<Gym> UnbookPlace(@RequestParam String OderId,@RequestBody BookPlaceRequest bookPlaceRequest){
        Result result=gymServer.UnbookPlace(OderId,bookPlaceRequest);
        return result;
    }

    //通过手机号查看用户所有订单
    @GetMapping("/ShowAllOderByTel")
    public Result<List<Order>> ShowAllOderByTel(String tel){
        Result result=userServer.ShowAllOderByTel(tel);
        return result;
    }

    @GetMapping("/GetUserInfo")
    public Result<User> getUserInfo(){
        Result result=userServer.getUserInfo();
        return result;
    }

    //用户修改信息
    @PostMapping("/editmessage")
    public Result<String> editmessage(@RequestParam String tel,@RequestParam String name,@RequestParam String sex){
        System.out.println("电话:" + tel + ", 姓名:" + name + ", 性别:" + sex);
        Result result=userServer.editmessage(tel,name,sex);
        return result;
    }

    //用户修改密码
    @PostMapping("/editpsw")
    public Result<String> editpsw(@RequestParam String tel,@RequestParam String oldpsw,@RequestParam String newpsw){
        Result result=userServer.editpsw(tel,oldpsw,newpsw);
        return result;
    }
}
